package com.ilxqx.framework.system.strategy.search;

import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;

/**
 * 相似条件策略类
 * @author venus
 */
public class LikeConditionStrategy implements ConditionStrategy {

    /**
     * 处理条件构建
     *
     * @param value   被比较值
     * @param path    比较路径（属性）
     * @param builder 条件构建器
     * @return 断言
     */
    @Override
    @Nullable
    @SuppressWarnings("unchecked")
    public Predicate handle(Object value, Path path, CriteriaBuilder builder) {
        if (value instanceof String) {
            String val = (String) value;
            if (StringUtils.isNotBlank(val)) {
                return builder.like(path, "%" + (String) value + "%");
            }
        }
        // 非字符串类型不能进行相似查找
        return null;
    }

}
